package patient

import (
	"api-gateway/basic/config"
	proto "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// ==================== 订单查询 ====================

func GetOrderList(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	var req request.OrderQueryReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetOrderList(ctx, &proto.GetOrderListRequest{
		UserId:    userID,
		OrderType: req.OrderType,
		Page:      req.Page,
		PageSize:  req.PageSize,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "获取订单列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": resp,
	})
}

func GetOrderDetail(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	var req struct {
		OrderID int64 `json:"orderId" binding:"required"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetOrderDetail(ctx, &proto.GetOrderDetailRequest{
		OrderId: req.OrderID,
		UserId:  userID,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "获取订单详情失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": resp.Order,
	})
}

func GetAppointmentOrderList(c *gin.Context)  { getTypedOrderList(c, 1) }
func GetConsultationOrderList(c *gin.Context) { getTypedOrderList(c, 2) }
func GetPrescriptionOrderList(c *gin.Context) { getTypedOrderList(c, 3) }

func getTypedOrderList(c *gin.Context, orderType int32) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	var req struct {
		Page     int32 `json:"page" binding:"min=1"`
		PageSize int32 `json:"pageSize" binding:"max=100,min=1"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.GetOrderList(ctx, &proto.GetOrderListRequest{
		UserId:    userID,
		OrderType: orderType,
		Page:      req.Page,
		PageSize:  req.PageSize,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "获取订单列表失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  resp.Message,
		"data": resp,
	})
}

// ==================== 订单操作 ====================

func CancelOrder(c *gin.Context) {
	userID := c.GetInt64("userId")
	if userID == 0 {
		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
		return
	}

	var req request.CancelOrderReq
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	resp, err := config.PatientClient.CancelOrder(ctx, &proto.CancelOrderRequest{
		OrderId: req.OrderID,
		UserId:  userID,
		Reason:  req.Reason,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "取消订单失败"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": resp.Message})
}

//func PayOrder(c *gin.Context) {
//	userID := c.GetInt64("userId")
//	if userID == 0 {
//		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
//		return
//	}
//
//	var req struct {
//		OrderID int64  `json:"orderId" binding:"required"`
//		PayType string `json:"payType" binding:"oneof=wechat alipay"`
//	}
//	if err := c.ShouldBindJSON(&req); err != nil {
//		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
//		return
//	}
//
//	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
//	defer cancel()
//
//	resp, err := config.PatientClient.PayOrder(ctx, &proto.PayOrderRequest{
//		OrderId: req.OrderID,
//		UserId:  userID,
//		PayType: req.PayType,
//	})
//	if err != nil {
//		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "创建支付失败"})
//		return
//	}
//
//	c.JSON(http.StatusOK, gin.H{
//		"code": 200,
//		"msg":  resp.Message,
//		"data": resp.PayInfo,
//	})
//}
//
//func RefundOrder(c *gin.Context) {
//	userID := c.GetInt64("userId")
//	if userID == 0 {
//		c.JSON(http.StatusOK, gin.H{"code": 401, "msg": "用户未登录"})
//		return
//	}
//
//	var req request.RefundOrderReq
//	if err := c.ShouldBindJSON(&req); err != nil {
//		c.JSON(http.StatusOK, gin.H{"code": 400, "msg": "参数错误"})
//		return
//	}
//
//	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
//	defer cancel()
//
//	resp, err := config.PatientClient.RefundOrder(ctx, &proto.RefundOrderRequest{
//		OrderId: req.OrderID,
//		UserId:  userID,
//		Reason:  req.Reason,
//	})
//	if err != nil {
//		c.JSON(http.StatusOK, gin.H{"code": 500, "msg": "提交退款申请失败"})
//		return
//	}
//
//	c.JSON(http.StatusOK, gin.H{
//		"code": 200,
//		"msg":  resp.Message,
//		"data": gin.H{"orderId": req.OrderID},
//	})
//}
